public class Barrier
{
	int count;
	int maxCapacity;
	Movie movie;

	public Barrier( int maxCapacity, Movie movie )
	{
		count = 0;
		this.maxCapacity = maxCapacity;
		this.movie=movie;
	}

	public synchronized void addToBarrier()
	{
		count++;
		
		if( count!= maxCapacity || movie.theater.getFirstMovie()!=movie )
		{
			try
			{
				wait();
			} catch( InterruptedException e )
			{};
				
		}
		else if(count== maxCapacity && movie.theater.getFirstMovie()==movie)	
		{
			notifyAll();
			movie.isWatched=true;
			
			Barrier nextBarrier= movie.theater.getFirstMovie().barrier;
			nextBarrier.checkBarrier();
		}	 
			
	}
	
	public synchronized void checkBarrier()
	{
		
		if(count== maxCapacity )	
		{
			notifyAll();
			movie.isWatched=true;
			
			Barrier nextBarrier= movie.theater.getFirstMovie().barrier;
			nextBarrier.checkBarrier();
		}	 
			
	}
}
